package com.agileai.em.service.expenses;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.em.common.ProcessHelper;
import com.agileai.em.cxmodule.BizCodeUtil;
import com.agileai.em.cxmodule.EmEvectionManage;
import com.agileai.em.cxmodule.EmExpReimbursementManage;
import com.agileai.em.wsproxy.BizAttribute;
import com.agileai.em.wsproxy.HandleWorkItem;
import com.agileai.em.wsproxy.ProcessService;
import com.agileai.em.wsproxy.SubmitWorkItem;
import com.agileai.em.wsproxy.WorkItem;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.ws.BaseRestService;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

public class TodoTaskDataProviderImpl extends BaseRestService implements TodoTaskDataProvider{
	private static String FAIL = "fail";
	private static String SUCCESS = "success";
	
	
	@Override
	public String retrieveTaskList() {
		String responseText = FAIL;
		User user = (User)this.getUser();
		String userCode = user.getUserCode();
		String userId = user.getUserId();
		
		List<DataRow> waitList = new ArrayList<DataRow>();
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		List<DataRow> processList = emEvectionManage.getReiList(userId,"AUDITING");
		List<DataRow> overList = emEvectionManage.getReiList(userId,"PAID");
		
		ProcessService bpmService = ProcessHelper.instance().getBPMService();
		List<WorkItem> taskItemList = bpmService.findWorkItems(userCode);
		processRecords(taskItemList, waitList);

		try {
		JSONObject jsonObj = new JSONObject();
		JSONArray waitjsonArr = new JSONArray();
		for(int i = 0; i < waitList.size(); i++){
			DataRow row = waitList.get(i);
			JSONObject tempJsonObj = new JSONObject();
			Date startDate = (Date) row.get("WFIP_LAUCHER_TIME");
			String commitTime = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING, startDate);
			Date handleDate = (Date) row.get("WFIP_OPERATER_TIME");
			String handleTime = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING, handleDate);
			
			tempJsonObj.put("id", row.stringValue("WFIP_BUSINESS_ID"));
			tempJsonObj.put("theme", row.stringValue("WFIP_TITLE"));
			tempJsonObj.put("submitterName", row.stringValue("WFIP_OPERATER_NAME"));
			tempJsonObj.put("commitTime", commitTime);
			tempJsonObj.put("state", row.stringValue("WFP_CODE"));
			tempJsonObj.put("checkStateName", "");
			tempJsonObj.put("proposer", row.stringValue("WFIP_LAUCHER_NAME"));
			tempJsonObj.put("operator", row.stringValue("WFIP_OPERATER_NAME"));
			tempJsonObj.put("processTime", handleTime);
			tempJsonObj.put("mobileModal", row.stringValue("WFIP_MOBILE_BIZURL"));
			
			DataParam queryParam = new DataParam();
			queryParam.put("id",row.stringValue("WFIP_BUSINESS_ID"));
			if(row.stringValue("WFP_CODE").equals("Expreimbursement")){
				queryParam.put("daily","daily");
			}else{
				queryParam.put("evection","evection");
			}
			String money = emEvectionManage.getMoneyById(queryParam);
			
			
			tempJsonObj.put("money", money);
			tempJsonObj.put("type",row.stringValue("WFP_NAME"));
			tempJsonObj.put("commitType", "");
			tempJsonObj.put("checkNodeName", row.stringValue("WFA_NAME"));
			
			tempJsonObj.put("WFP_ID", row.stringValue("WFP_ID"));
			tempJsonObj.put("WFA_CODE", row.stringValue("WFA_CODE"));
			tempJsonObj.put("WFIP_ID", row.stringValue("WFIP_ID"));
			
			waitjsonArr.put(tempJsonObj);
		}
		
		JSONArray processJsonArr = new JSONArray();
		
		for(int i = 0; i < processList.size(); i++){
			DataRow row = processList.get(i);
			JSONObject tempJsonObj = new JSONObject();
			Date startDate = (Date) row.get("commitTime");
			String commitTime = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, startDate);
			
			ProcessHelper processHelper = ProcessHelper.instance();
		    ProcessService tempBpmService = processHelper.getBPMService();
		    WorkItem workItem = this.getActivityName(tempBpmService, row.stringValue("id"));
		    String activityName = "";
		    if(workItem != null){
				/*String laucherName = workItem.getLaucherName();
				String laucheTime = workItem.getLaucheTime();*/
				activityName = workItem.getActivityName();
			}
			
			tempJsonObj.put("id", row.stringValue("id"));
			tempJsonObj.put("theme", row.stringValue("theme"));
			tempJsonObj.put("submitterName", row.stringValue("submitterName"));
			tempJsonObj.put("commitTime", commitTime);
			tempJsonObj.put("state", row.stringValue("state"));
			tempJsonObj.put("checkStateName", row.stringValue("checkStateName"));
			tempJsonObj.put("money", row.stringValue("money"));
			tempJsonObj.put("type", row.stringValue("type"));
			tempJsonObj.put("commitType",row.stringValue("checkStateName"));
			tempJsonObj.put("checkNodeName", activityName);
			tempJsonObj.put("WFP_CODE",row.stringValue("WFP_CODE"));
			
			processJsonArr.put(tempJsonObj);
		}
		
		JSONArray overJsonArr = new JSONArray();
		
		for(int i = 0; i < overList.size(); i++){
			DataRow row = overList.get(i);
			JSONObject tempJsonObj = new JSONObject();
			Date startDate = (Date) row.get("commitTime");
			String commitTime = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, startDate);
			
			ProcessHelper processHelper = ProcessHelper.instance();
		    ProcessService tempBpmService = processHelper.getBPMService();
		    WorkItem workItem = this.getActivityName(tempBpmService, row.stringValue("id"));
		    String activityName = "";
		    if(workItem != null){
				/*String laucherName = workItem.getLaucherName();
				String laucheTime = workItem.getLaucheTime();*/
				activityName = workItem.getActivityName();
			}
			
			tempJsonObj.put("id", row.stringValue("id"));
			tempJsonObj.put("theme", row.stringValue("theme"));
			tempJsonObj.put("submitterName", row.stringValue("submitterName"));
			tempJsonObj.put("commitTime", commitTime);
			tempJsonObj.put("state", row.stringValue("state"));
			tempJsonObj.put("checkStateName", row.stringValue("checkStateName"));
			tempJsonObj.put("money", row.stringValue("money"));
			tempJsonObj.put("type", row.stringValue("type"));
			tempJsonObj.put("commitType",row.stringValue("checkStateName"));
			tempJsonObj.put("checkNodeName", activityName);
			tempJsonObj.put("WFP_CODE", row.stringValue("WFP_CODE"));
			
			overJsonArr.put(tempJsonObj);
		}
		
		jsonObj.put("wait", waitjsonArr);
		jsonObj.put("process",processJsonArr);
		jsonObj.put("over", overJsonArr);
		responseText = jsonObj.toString();
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return responseText;
	}
	
	@Override
	public String DailyTodoTaskSubmit(DataParam param) {
		//ProcessService bpmService = ProcessHelper.instance().getBPMService();
		
		EmExpReimbursementManage emExpReimbursementManage = this.lookupService(EmExpReimbursementManage.class);
		
		
		String responseText = FAIL;
		try {
			User user = (User) getUser();
			DataParam queryParam = new DataParam();
			queryParam.put("EXP_APP_ID",KeyGenerator.instance().genKey());
			queryParam.put("ER_ID",param.stringValue("id")); 
			queryParam.put("EXP_APP_PERSON",user.getUserId());
			queryParam.put("EXP_APP_OPINION",param.stringValue("desc")); 
			queryParam.put("EXP_APP_RESULT",param.stringValue("result"));   
			queryParam.put("EXP_APP_TIME",new Date());
			
			
			
			BizAttribute bizAttribute = new BizAttribute();
			bizAttribute.setCode("AppoperType");
			bizAttribute.setValue(param.stringValue("result"));  
			BizAttribute bizAttribute1 = new BizAttribute();
			bizAttribute1.setCode("totalMoney");
			bizAttribute1.setValue(param.stringValue("money")); 
			List<BizAttribute> attributeList = new ArrayList<BizAttribute>();
			attributeList.add(bizAttribute);
			attributeList.add(bizAttribute1);
			
			ProcessHelper processHelper = ProcessHelper.instance();
			ProcessService bpmService = processHelper.getBPMService();
			String processId = param.stringValue("WFP_ID");         
			String processInstId = param.stringValue("WFIP_ID");    
			String activityCode = param.stringValue("WFA_CODE"); 
			
			String activityInstId = bpmService.getActivityInstId(processInstId, activityCode);
			queryParam.put("EXP_ACT_INST_ID",activityInstId);
			emExpReimbursementManage.createApproveRecord(queryParam);
			
			SubmitWorkItem processWorkItem = processHelper.createSubmitWorkItem(processId, processInstId, activityCode);
			if(StringUtil.isNullOrEmpty(attributeList.get(0).getCode())){
				attributeList = processWorkItem.getAttributeList();
			}
			processWorkItem.setUserCode(user.getUserCode());
			processWorkItem.setTitle(param.stringValue("title"));    
			processWorkItem.getAttributeList().addAll(attributeList);
			bpmService.submitProcess(processWorkItem);
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		
		return responseText;
	}
	
	@Override
	public String DailyTodoTaskPay(DataParam param) {
		EmExpReimbursementManage emExpReimbursementManage = this.lookupService(EmExpReimbursementManage.class);
		String responseText = FAIL;
		
		try {
			User user = (User) getUser();
			DataParam newParam = new DataParam();
			newParam.put("ER_ID",param.stringValue("id"));
			newParam.put("ER_STATE", "PAID");
			emExpReimbursementManage.changeStateRecord(newParam);
			
			List<BizAttribute> attributeList = new ArrayList<BizAttribute>();
			BizAttribute bizAttribute = new BizAttribute();
	   		bizAttribute.setCode("userId");
	   		bizAttribute.setValue(user.getUserId());
	   		attributeList.add(bizAttribute);
	   		
	   		ProcessHelper processHelper = ProcessHelper.instance();
			ProcessService bpmService = processHelper.getBPMService();
			String processId = param.stringValue("WFP_ID");
			String processInstId = param.stringValue("WFIP_ID");
			String activityCode = param.stringValue("WFA_CODE");
			SubmitWorkItem processWorkItem = processHelper.createSubmitWorkItem(processId, processInstId, activityCode);
			if(StringUtil.isNullOrEmpty(attributeList.get(0).getCode())){
				attributeList = processWorkItem.getAttributeList();
			}
			processWorkItem.setUserCode(user.getUserCode());
			processWorkItem.getAttributeList().addAll(attributeList);
			bpmService.submitProcess(processWorkItem);
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		
		return responseText;
	}
	
	private void processRecords(List<WorkItem> taskItemList,List<DataRow> rsList){
		if (taskItemList != null){
			for (int i=0;i < taskItemList.size();i++){
				WorkItem taskItem = taskItemList.get(i);
				DataRow row = new DataRow();
				row.put("WFIP_ID",taskItem.getProcInstanceId());
				row.put("WFP_ID",taskItem.getProcessId());
				row.put("WFA_CODE",taskItem.getActivityCode());
				row.put("WFIP_BUSINESS_ID",taskItem.getBusinessId());
				row.put("WFP_CODE", taskItem.getProcessCode());
				row.put("WFP_NAME",taskItem.getProcessName());
				row.put("WFA_NAME",taskItem.getActivityName());
				row.put("WFIP_TITLE",taskItem.getTitle());
				row.put("WFIP_BIZURL",taskItem.getBusinessURL());
				row.put("WFIP_MOBILE_BIZURL",taskItem.getMobileBizURL());
				row.put("WFIP_TITLE",taskItem.getTitle());
				row.put("WFIP_LAUCHER_NAME",taskItem.getLaucherName());
				row.put("WFIP_LAUCHER_TIME",DateUtil.getDateTime(taskItem.getLaucheTime()));
				row.put("WFIP_OPERATER_NAME",taskItem.getSubmiterName());
				row.put("WFIP_OPERATER_TIME",DateUtil.getDateTime(taskItem.getSubmiteTime()));
				rsList.add(row);
			}			
		}
	}
	
	
	
	
	private WorkItem getActivityName(ProcessService bpmService,String id){
		WorkItem workItem = null;
		List<WorkItem> taskItemList = bpmService.findWorkItems("");
		for (int i = 0; i < taskItemList.size(); i++) {
			WorkItem taskItem = taskItemList.get(i);
			String bizId = taskItem.getBusinessId();
			if(bizId.equals(id)){
				workItem =  taskItem;
			}
		}
		return workItem;
	}

	@Override
	public String DailyTodoTaskAgain(DataParam detailInfoParam) {
		String responseText = FAIL;
		
		User user = (User) this.getUser();
		EmExpReimbursementManage emExpReimbursementManage = this.lookupService(EmExpReimbursementManage.class);
		DataParam param = new DataParam();
		param.put("ER_PERSON",user.getUserId());
		param.put("ER_STYLE", detailInfoParam.stringValue("style"));
		param.put("ER_TITLE", detailInfoParam.stringValue("theme"));
		param.put("ER_MONEY", detailInfoParam.stringValue("money"));
		param.put("ER_REMARKS", detailInfoParam.stringValue("desc"));
		param.put("ER_STATE", "UNSUBMITTED");
		
		String strDate = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL, new Date());
		Date date = DateUtil.getDate(strDate);
		String state = "AUDITING";
		param.put("ER_ID",detailInfoParam.stringValue("id"));
		param.put("ER_DATE",date);
		param.put("ER_STATE", state);
		emExpReimbursementManage.updateRecord(param);
		
		List<BizAttribute> attributeList = new ArrayList<BizAttribute>();
		String processId = detailInfoParam.getString("WFP_ID");
		String processInstId = detailInfoParam.getString("WFIP_ID");
		String activityCode = detailInfoParam.getString("WFA_CODE");	
    	
		ProcessHelper processHelper = ProcessHelper.instance();
		ProcessService bpmService = processHelper.getBPMService();	
		SubmitWorkItem processWorkItem = processHelper.createSubmitWorkItem(processId, processInstId, activityCode);	
		attributeList = processWorkItem.getAttributeList();
		processWorkItem.setUserCode(user.getUserCode());	
    		
		processWorkItem.getAttributeList().addAll(attributeList);
		bpmService.submitProcess(processWorkItem);
    		
		responseText = SUCCESS;	
		return responseText;
	}

	@Override
	public String DailyTodoTaskCancel(DataParam detailInfoParam) {
		String responseText = FAIL;
		User user = (User) this.getUser();
		EmExpReimbursementManage emExpReimbursementManage = this.lookupService(EmExpReimbursementManage.class);
		DataParam param = new DataParam();
		param.put("ER_PERSON",user.getUserId());
		param.put("ER_STYLE", detailInfoParam.stringValue("style"));
		param.put("ER_TITLE", detailInfoParam.stringValue("theme"));
		param.put("ER_MONEY", detailInfoParam.stringValue("money"));
		param.put("ER_REMARKS", detailInfoParam.stringValue("desc"));
		param.put("ER_STATE", "CANCEL");
		
		String strDate = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL, new Date());
		Date date = DateUtil.getDate(strDate);
		String state = "CANCEL";
		param.put("ER_ID",detailInfoParam.stringValue("id"));
		param.put("ER_DATE",date);
		param.put("ER_STATE", state);
		emExpReimbursementManage.updateRecord(param);
		
		ProcessHelper processHelper = ProcessHelper.instance();
   		ProcessService processService = processHelper.getBPMService();
		String processId = detailInfoParam.getString("WFP_ID");
		String processInstId = detailInfoParam.getString("WFIP_ID");
		
		HandleWorkItem handleWorkItem = new HandleWorkItem();
		handleWorkItem.setProcessId(processId);
		handleWorkItem.setProcessInstanceId(processInstId);
		handleWorkItem.setUserCode(user.getUserCode());
		processService.terminateProcess(handleWorkItem);
		responseText = SUCCESS;
		
		return responseText;
	}
	
	@Override
	public String expensesTodoTaskSubmit(DataParam param) {
		String responseText = FAIL;
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		try {
			
			
			User user = (User) getUser();
			DataParam queryParam = new DataParam();
			queryParam.put("EVE_APP_ID",KeyGenerator.instance().genKey());
			queryParam.put("EVE_ID",param.stringValue("id"));
			queryParam.put("EVE_APP_PERSON",user.getUserId());
			queryParam.put("EVE_APP_OPINION",param.stringValue("desc"));
			queryParam.put("EVE_APP_RESULT",param.stringValue("result"));
			queryParam.put("EVE_APP_TIME",new Date());

			
			ProcessHelper processHelper = ProcessHelper.instance();
			BizAttribute bizAttribute = new BizAttribute();
			bizAttribute.setCode("AppoperType");
			bizAttribute.setValue(param.stringValue("result"));
			BizAttribute bizAttribute1 = new BizAttribute();
			bizAttribute1.setCode("totalMoney");
			bizAttribute1.setValue(param.stringValue("money"));
			List<BizAttribute> attributeList = new ArrayList<BizAttribute>();
			attributeList.add(bizAttribute);
			attributeList.add(bizAttribute1);
			
			ProcessService bpmService = ProcessHelper.instance().getBPMService();
			String processId = param.stringValue("WFP_ID");
			String processInstId = param.stringValue("WFIP_ID");
			String activityCode = param.stringValue("WFA_CODE");
			
			String activityInstId = bpmService.getActivityInstId(processInstId, activityCode);
			queryParam.put("EVE_ACT_INST_ID",activityInstId);
			emEvectionManage.createApproveRecord(queryParam);
			
			String title = param.stringValue("title");
			SubmitWorkItem processWorkItem = processHelper.createSubmitWorkItem(processId, processInstId, activityCode);
			if(StringUtil.isNullOrEmpty(attributeList.get(0).getCode())){
				attributeList = processWorkItem.getAttributeList();
			}
			
			processWorkItem.setUserCode(user.getUserCode());
			processWorkItem.setTitle(title);
			processWorkItem.getAttributeList().addAll(attributeList);
			bpmService.submitProcess(processWorkItem);
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		responseText = SUCCESS;
		return responseText;
	}

	@Override
	public String expensesTodoTaskPay(DataParam param) {
		String responseText = FAIL;
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		User user = (User) getUser();
		DataParam newParam = new DataParam();
		newParam.put("EVE_ID",param.stringValue("id"));
		newParam.put("EVE_STATE", "PAID");
		emEvectionManage.changeStateRecord(newParam);
   		
		List<BizAttribute> attributeList = new ArrayList<BizAttribute>();
		BizAttribute bizAttribute = new BizAttribute();
   		bizAttribute.setCode("userId");
   		bizAttribute.setValue(user.getUserId());
		attributeList.add(bizAttribute);
		String processId = param.getString("WFP_ID");
		String processInstId = param.getString("WFIP_ID");
		String activityCode = param.getString("WFA_CODE");
		
		ProcessHelper processHelper = ProcessHelper.instance();
		ProcessService bpmService = processHelper.getBPMService();
		SubmitWorkItem processWorkItem = processHelper.createSubmitWorkItem(processId, processInstId, activityCode);
		if(StringUtil.isNullOrEmpty(attributeList.get(0).getCode())){
			attributeList = processWorkItem.getAttributeList();
		}
		processWorkItem.setUserCode(user.getUserCode());
		processWorkItem.getAttributeList().addAll(attributeList);
		bpmService.submitProcess(processWorkItem);
		responseText = SUCCESS;
		return responseText;
	}

	@Override
	public String expensesTodoTaskAgain(DataParam detailInfoParam) {
		String responseText = FAIL;
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		User user = (User) this.getUser();
		String reiListStr = detailInfoParam.stringValue("expenseList");
		String EVE_ID = detailInfoParam.stringValue("id");
		
		String PC_ID = detailInfoParam.stringValue("projectId");
		String EVE_APPLICATION = user.getUserId();
		String EVE_TOGETHER = detailInfoParam.stringValue("togetherPerson");
		String EVE_START_TIME =this.dateFormat( detailInfoParam.stringValue("startTime"));
		String EVE_OVER_TIME = this.dateFormat( detailInfoParam.stringValue("endTime"));
		String EVE_REIMBURSEMENT_TIME = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, new Date());
		String EVE_DAYS = detailInfoParam.stringValue("evectionDay");
		String EVE_REASON = detailInfoParam.stringValue("desc");
		String EVE_SUBSIDY = detailInfoParam.stringValue("subsidy");
		String EVE_TOTAL_MONEY = detailInfoParam.stringValue("money");
		
		DataParam param = new DataParam();
		param.put("EVE_ID", EVE_ID);
		param.put("PC_ID", PC_ID);
		param.put("EVE_APPLICATION", EVE_APPLICATION);
		param.put("EVE_TOGETHER", EVE_TOGETHER);
		param.put("EVE_START_TIME", EVE_START_TIME);
		param.put("EVE_OVER_TIME", EVE_OVER_TIME);
		param.put("EVE_REIMBURSEMENT_TIME", EVE_REIMBURSEMENT_TIME);
		param.put("EVE_DAYS", EVE_DAYS);
		param.put("EVE_REASON", EVE_REASON);
		param.put("EVE_SUBSIDY", EVE_SUBSIDY);
		param.put("EVE_TOTAL_MONEY", EVE_TOTAL_MONEY);
		if(param.get("EVE_TOTAL_MONEY").isEmpty()){
			param.put("EVE_TOTAL_MONEY","0.00");
		}
		
		param.put("EVE_STATE", "AUDITING");
		
		param.put("sdate",DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, new Date())+" 00:00:00");
		param.put("edate",DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, new Date())+" 23:59:59");
		
		List<DataRow> list = emEvectionManage.findMasterRecords(param);
		String bizCode = BizCodeUtil.create("EVE", list, BizCodeUtil.withPrefix);
		param.put("EVE_CODE",bizCode);
		
		emEvectionManage.updateMasterRecord(param);
   		//保存清单
		try {
			JSONArray reiListArr = new JSONArray(reiListStr);
			for(int i = 0; i < reiListArr.length(); i++){
				JSONObject jsonObj = reiListArr.getJSONObject(i);
				String EXP_ID = (String) jsonObj.get("expId");
				//String EVE_ID = (String) jsonObj.get("");
				String EXP_DEPARTURE = (String) jsonObj.get("departure");
				String EXP_DESTINATION = (String) jsonObj.get("destination");
				String EXP_START_TIME =this.dateFormat( (String) jsonObj.get("sdate"));
				String EXP_END_TIME =this.dateFormat( (String) jsonObj.get("edate"));
				String EXP_TRANSPORTATION_WAY = (String) jsonObj.get("transportation");
				String EXP_TRANSPORTATION_FEE =  jsonObj.get("transportationFee").toString();
				String EXP_HOREL_FEE =  jsonObj.get("horelFee").toString();
				String EXP_OTHER_FEE =  jsonObj.get("otherFee").toString();
				String EXP_REMARKS = (String) jsonObj.get("remarks");
				 
				DataParam reiListParam = new DataParam();
				
				reiListParam.put("EVE_ID", EVE_ID);
				reiListParam.put("EXP_DEPARTURE", EXP_DEPARTURE);
				reiListParam.put("EXP_DESTINATION", EXP_DESTINATION);
				reiListParam.put("EXP_START_TIME", EXP_START_TIME);
				reiListParam.put("EXP_END_TIME", EXP_END_TIME);
				reiListParam.put("EXP_TRANSPORTATION_WAY", EXP_TRANSPORTATION_WAY);
				reiListParam.put("EXP_TRANSPORTATION_FEE", EXP_TRANSPORTATION_FEE);
				reiListParam.put("EXP_HOREL_FEE", EXP_HOREL_FEE);
				reiListParam.put("EXP_OTHER_FEE", EXP_OTHER_FEE);
				reiListParam.put("EXP_REMARKS", EXP_REMARKS);
				
				if(reiListParam.get("EXP_TRANSPORTATION_FEE").isEmpty()){
					reiListParam.put("EXP_TRANSPORTATION_FEE","0.00");
				}
				if(reiListParam.get("EXP_HOREL_FEE").isEmpty()){
					reiListParam.put("EXP_HOREL_FEE","0.00");
				}
				if(reiListParam.get("EXP_OTHER_FEE").isEmpty()){
					reiListParam.put("EXP_OTHER_FEE","0.00");
				}
				
				if(StringUtil.isNullOrEmpty(EXP_ID)){
					EXP_ID = KeyGenerator.instance().genKey();
					reiListParam.put("EXP_ID", EXP_ID);
					emEvectionManage.createSubRecord("EmExpenses", reiListParam);
				}else{
					reiListParam.put("EXP_ID", EXP_ID);
					emEvectionManage.updateSubRecord("EmExpenses", reiListParam);
				}
				
			}
			
			String masterRecordId = param.get("EVE_ID");
			emEvectionManage.computeTotalMoney(masterRecordId);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List<BizAttribute> attributeList = new ArrayList<BizAttribute>();
		String processId = detailInfoParam.getString("WFP_ID");
		String processInstId = detailInfoParam.getString("WFIP_ID");
		String activityCode = detailInfoParam.getString("WFA_CODE");
		
		ProcessHelper processHelper = ProcessHelper.instance();
		ProcessService bpmService = processHelper.getBPMService();
		SubmitWorkItem processWorkItem = processHelper.createSubmitWorkItem(processId, processInstId, activityCode);
		attributeList = processWorkItem.getAttributeList();
		processWorkItem.setUserCode(user.getUserCode());
		processWorkItem.getAttributeList().addAll(attributeList);
		bpmService.submitProcess(processWorkItem);
		
		responseText = SUCCESS;
		return responseText;
	}

	@Override
	public String expensesTodoTaskCancel(DataParam detailInfoParam) {
		String responseText = FAIL;
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		User user = (User) getUser();
		try {
			DataParam param = new DataParam();
			String EVE_ID = detailInfoParam.stringValue("id");
			param.put("EVE_STATE", "CANCEL");
			param.put("EVE_ID", EVE_ID);
			emEvectionManage.updateMasterRecordState(param);
			ProcessHelper processHelper = ProcessHelper.instance();
	   		ProcessService processService = processHelper.getBPMService();
			String processId = detailInfoParam.stringValue("WFP_ID");
			String processInstId = detailInfoParam.stringValue("WFIP_ID");
			
			HandleWorkItem handleWorkItem = new HandleWorkItem();
			handleWorkItem.setProcessId(processId);
			handleWorkItem.setProcessInstanceId(processInstId);
			handleWorkItem.setUserCode(user.getUserCode());
			processService.terminateProcess(handleWorkItem);
			responseText = SUCCESS;	
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return responseText;
	}

	private String dateFormat(String dateStr){
		String result = "";
		if(StringUtil.isNotNullNotEmpty(dateStr)){
			try {
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				Date date = formatter.parse(dateStr);
				result = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING,date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
    
}
